﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EyFramework.Maintenance
{
    public partial class FrmScreenProtect : Form
    {
        //程序启动路径，与生成程序的exe文件在同一目录下
        public String exePath = Application.StartupPath;

        private string imageUrl = "http://192.168.0.161/";

        private int imageIndex = 1;

        //窗口左上角位置
        public Point m_formAutographShowPos;

        public FrmScreenProtect()
        {
            InitializeComponent();
            //设置窗体尺寸
            this.Width = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
            this.ClientSize = new System.Drawing.Size(Width, Height);
            //计算初始位置
            m_formAutographShowPos.X = 0;
            m_formAutographShowPos.Y = 0;
            Location = m_formAutographShowPos;
            //窗体背景图
            this.BackgroundImage = Image.FromFile(exePath + "\\bgimage\\bg1.jpg");
        }

        private void FrmScreenProtect_MouseMove(object sender, MouseEventArgs e)
        {
        }

        private void FrmScreenProtect_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.timer1.Enabled = true;
            this.timeChangeBackground.Enabled = true;
            refreshTime();
            this.TopMost = true;
            this.BringToFront();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            refreshTime();
        }

        private void refreshTime()
        {
            lbl_time_hour.Text = DateTime.Now.ToString("HH:mm");
            lbl_time_week.Text = DateTime.Now.ToString("yyyy年MM月dd日 dddd");
        }

        private void changeBackGroundImage()
        {
            string imagePath = imageUrl + "/bg" + imageIndex + ".jpg";
            Image imgBack;
            try
            {
                imgBack =
                    Image.FromStream(System.Net.WebRequest.Create(imagePath).GetResponse().GetResponseStream());
                if (imgBack == null)
                {
                    imgBack = Image.FromFile(exePath + "\\bgimage\\bg" + imageIndex + ".jpg");
                }
            }
            catch
            {
                imgBack = Image.FromFile(exePath + "\\bgimage\\bg" + imageIndex + ".jpg");
            }
            this.BackgroundImage = imgBack;
        }

        private void timeChangeBackground_Tick(object sender, EventArgs e)
        {
            imageIndex++;
            if (imageIndex > 4)
            {
                imageIndex = 1;
            }
            changeBackGroundImage();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            PublicGlobal.HideScreenProtect();
        }
    }
}